package org.jwos.platform.dao.internal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;

import org.jwos.platform.dao.UserDAO;
import org.jwos.platform.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.transaction.annotation.Transactional;

public class StubUserDAO implements UserDAO {
	
	private SimpleJdbcTemplate simpleJdbcTemplate;
	
	private SimpleJdbcInsert insert;
	
	@Autowired
	public void init(DataSource dataSource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

		this.insert = new SimpleJdbcInsert(dataSource)
			.withTableName("user");		
	}

	@Transactional
	public void deleteUser(String userId) {
	}

	@Transactional
	public void insertUser(User user) {
		user.setId(user.getUserId());
		user.setPassword(user.getUserId());
		this.insert.execute(createPetParameterSource(user));
	}
	
	@Transactional(readOnly=true)
	public User queryUserByEmail(String email) {
		return null;
	}
	
	@Transactional(readOnly=true)
	public User queryUserById(String id) {
		User user = null;
		
		try {
			user = this.simpleJdbcTemplate.queryForObject(
					"SELECT id, userId, userName, password, email, enable FROM user WHERE id = ? ",
					new JdbcUserRowMapper(),
					id);
		} catch (EmptyResultDataAccessException ex) {
			throw new ObjectRetrievalFailureException(User.class, id);
		}
		
		return user;
	}

	@Transactional(readOnly=true)
	public User queryUserByUserId(String userId) {
		User user = null;
		
		try {
			user = this.simpleJdbcTemplate.queryForObject(
					"SELECT id, userId, userName, password, email, enable FROM user WHERE userId = ? ",
					new JdbcUserRowMapper(),
					userId);
		} catch (EmptyResultDataAccessException ex) {
			throw new ObjectRetrievalFailureException(User.class, userId);
		}
		
		return user;
	}
	
	@Transactional(readOnly=true)
	public List queryAllUsers() {
		List users = new ArrayList();
		
		try {
			users = this.simpleJdbcTemplate.query(
					"SELECT id, userId, userName, password, email, enable FROM user ",
					new JdbcUserRowMapper(),
					new HashMap());
		} catch (EmptyResultDataAccessException ex) {
			throw new ObjectRetrievalFailureException(User.class,"user");
		}
		
		return users;
	}

	@Transactional
	public void updateUser(User user) {
		this.simpleJdbcTemplate.update("UPDATE user SET userName=?, email=?,locale=? WHERE id=? ",
			new Object[]{user.getUserName(),user.getEmail(),user.getLocale(),user.getId()}
		);
	}
	
	private MapSqlParameterSource createPetParameterSource(User user) {
		return new MapSqlParameterSource()
			.addValue("id", user.getId())
			.addValue("userId", user.getUserId())
			.addValue("userName", user.getUserName())
			.addValue("password", user.getPassword())
			.addValue("email", user.getEmail())
			.addValue("enable", Boolean.toString(user.isEnable()));
	}
	
	private class JdbcUserRowMapper implements ParameterizedRowMapper<User> {

		public User mapRow(ResultSet rs, int rownum) throws SQLException {
			User user = new User();
			user.setId(rs.getString("id"));
			user.setUserId(rs.getString("userId"));
			user.setUserName(rs.getString("userName"));
			user.setPassword(rs.getString("password"));
			user.setEmail(rs.getString("email"));
			user.setEnable(Boolean.parseBoolean(rs.getString("enable")));
			return user;
		}
	}
}
